package com.piece.module.generator.service;

import com.piece.core.framework.enums.StatusType;
import com.piece.core.framework.enums.sql.SqlKey;
import com.piece.core.db.util.DataBaseUtil;
import com.piece.core.jpa.repository.BaseService;
import com.piece.core.framework.support.bootstrap.Order;
import com.piece.core.framework.support.convert.Convert;
import com.piece.core.framework.util.object.BeanUtil;
import com.piece.core.framework.util.string.StringUtil;
import com.piece.module.generator.model.TbSysDb;
import com.piece.module.generator.repository.TbSysDbRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class TbSysDbService extends BaseService<TbSysDb, String> {

    @Resource
    private TbSysDbRepository tbSysDbRepository;

    @PostConstruct
    public void initRepository() {
        setRepository(tbSysDbRepository);
    }

    @Override
    public TbSysDb insert(TbSysDb tbSysDb) {
        // 删除数据源
        DataBaseUtil.deleteDataSource(tbSysDb.getDbKey());
        // 创建数据源
        Map dataSource = new HashMap();
        BeanUtil.beanToMap(tbSysDb, dataSource);
        DataBaseUtil.createDataSource(dataSource);
        tbSysDb = tbSysDbRepository.insert(tbSysDb);
        return tbSysDb;
    }

    @Override
    public void update(TbSysDb tbSysDb) throws Exception {
        // 删除数据源
        DataBaseUtil.deleteDataSource(tbSysDb.getDbKey());
        // 创建数据源
        Map dataSource = new HashMap();
        BeanUtil.beanToMap(tbSysDb, dataSource);
        DataBaseUtil.createDataSource(dataSource);
        tbSysDbRepository.update(tbSysDb);
    }

    @Override
    public void delete(Serializable id) throws Exception {
        String dbKey = Convert.toStr(id);
        // 删除数据源
        DataBaseUtil.deleteDataSource(dbKey);
        // tbSysDbRepository.updateByProperties("dbKey", dbKey, "status", Constants.STATUS_DELETE);
        tbSysDbRepository.deleteByProperties("dbKey", dbKey);
    }

    @Override
    public int delete(Serializable[] ids) throws Exception {
        for (Serializable id : ids) {
            delete(id);
        }
        return ids.length;
    }

    /**
     * 根据dbKey获取数据源
     */
    public TbSysDb findByDBKey(String dbKey) {
        Map<String, Object> param = new HashMap<>();
        param.put("dbKey", dbKey);
        param.put("status", StatusType.NORMAL.getCode());
        return tbSysDbRepository.findOneByCondition(param);
    }

    /**
     * 校验数据源标识是否唯一
     *
     * @param dbKey 数据源名称
     * @return true: 通过 false: 重复
     */
    public boolean checkDbKeyUnique(String dbKey) {
        String hql = "from TbSysDb where dbKey = ?0 and status = ?1";
        Object[] params = new Object[]{dbKey, StatusType.NORMAL.getCode()};
        long count = tbSysDbRepository.count(hql, params, true);
        return count <= 0;
    }

    /**
     * 校验数据源名称是否唯一
     *
     * @param dbKey  数据源标识
     * @param dbName 数据源名称
     * @return true: 通过 false: 重复
     */
    public boolean checkNameUnique(String dbKey, String dbName) {
        String hql = "from TbSysDb where dbName = ?0 and status = ?1";
        Object[] params = new Object[]{dbName, StatusType.NORMAL.getCode()};
        if (StringUtil.isNotEmpty(dbKey)) {
            hql = "from TbSysDb where dbKey != ?0 and dbName = ?1 and status = ?2";
            params = new Object[]{dbKey, dbName, StatusType.NORMAL.getCode()};
        }
        long count = tbSysDbRepository.count(hql, params, true);
        return count <= 0;
    }

    @Override
    protected List<Order> generateDefaultOrder() {
        List<Order> orders = new ArrayList<>();
        orders.add(new Order("dbKey", SqlKey.DESC));
        return orders;
    }
}
